package com.zy.web.controller.bs;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zy.entity.bs.Order;
import com.zy.pojo.query.bs.OrderQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.pojo.vos.common.ResultVo;
import com.zy.service.bs.OrderService;
import com.zy.web.controller.base.BaseController;
import com.zy.web.utils.ResultUtil;

import java.io.Serializable;

@RequestMapping({"bs/order","m/bs/order"})
@Controller
public class OrderController extends BaseController {

	@Autowired
	private OrderService orderService;

	/**
	 * 分页
 	 * 
 	 * @param orderQuery
	 * @param start
	 * @param limit
	 * @return
	 */
	@RequestMapping("pageOrder")
	@ResponseBody
	public PageVo pageOrder(OrderQuery orderQuery, Integer start, Integer limit) {
		return this.orderService.pageOrder(orderQuery, start, limit);
	}

	/**
	 * 新增
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "saveOrder", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo saveOrder(Order order) {
		try {
			this.orderService.saveOrder(order);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}
	
	/**
	 * 更新
	 * 
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "updateOrder", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo updateOrder(Order order) {
		try {
			this.orderService.updateOrder(order);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "removeOrder", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo removeOrder(long id) {
		try {
			this.orderService.removeOrder(id);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	/**
	 * 恢复
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "recover", method = RequestMethod.POST)
	@ResponseBody
	public ResultVo recover(long id) {
		try {
			this.orderService.recover(id);
			return ResultUtil.success();
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	@RequestMapping(value = "getCustomerOrders", method = RequestMethod.GET)
	@ResponseBody
	public ResultVo getCustomerOrders() {
		try {
			OrderQuery orderQuery = new OrderQuery();
			orderQuery.setCustomerId(this.getCurrentCustomer().getId());
			return ResultUtil.success((Serializable) this.orderService.getCustomerOrders(orderQuery));
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

	@RequestMapping(value = "getCustomerOrderInfo")
	@ResponseBody
	public ResultVo getCustomerOrderInfo(Long id) {
		try {
			return ResultUtil.success((Serializable) this.orderService.getByPrimaryKey(id));
		} catch (Exception e) {
			return ResultUtil.error(log, e);
		}
	}

}